package org.inasnet.user;

import java.io.DataInputStream;

import org.inasnet.Root;
import org.inasnet.user.Pack.MessageType;
import org.inasnet.util.Util;

/**
 * Class for getting data 
 * \see Pack, PackOut
 */
public class PackIn extends Pack
{
	private PackIn()
	{
		mType = MessageType.NULL.ordinal();
	}
	
	public PackIn( DataInputStream inputStream )
	{
		try
		{
			byte byteI = inputStream.readByte();
			//byte byteN = inputStream.readByte();
			byte byteS = inputStream.readByte();
			
			//Root.log( byteI + "-" + byteS );
			
			if( byteI != 'I' /* || byteN != 'N'*/ || byteS != 'S' )
			{
				throw new Exception( "Pack signature isn't wright!" );
			}
			
			mType         = inputStream.readByte();
			mMessageBytes = inputStream.readShort();
					
			if( mMessageBytes != 0 )
			{
				mMessage = new byte[ mMessageBytes ];
			
				inputStream.readFully( mMessage );
			}
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}
		
		Root.log( toString() );
	}
	
	public String toString()
	{
		return "Pack----IN---:  T:" + MessageType.values()[ mType ] + "; MBS:" + mMessageBytes + 
			"; MB: " + Util.bytesToString( mMessage, 10 );
	}
	
	public int getType()
	{
		return mType;
	}
	
	public int getMessageSize()
	{
		return mMessageBytes;
	}
	
	public byte [] getMessageBytes()
	{
		return mMessage;
	}
	
	public static PackIn createEmptyPack() 
	{
		return new PackIn();
	}
	
	int mType;
	int mMessageBytes;
	byte [] mMessage;
}
